<template>
  <div class="wrapper">
    <nut-address-list :show-bottom-button="false" swipe-edition :data="data" :options="dataOptions"
      @click-item="handleClickAddress">
      <template #itemAddr="{ item }">
        {{ item.fullAddress }}
      </template>
      <template #itemIcon="{ item }">
        <span></span>
      </template>
      <template #itemInfos="{ item }">
        <div>
          {{ item.addressName }} {{ item.phone }}
        </div>
      </template>
    </nut-address-list>
    <nut-toast ref="toastRef" />
  </div>
</template>
<script setup lang="ts">
import { setStoreAddressId } from '@/api/trade'
import { onPullDownRefresh, onShow } from '@dcloudio/uni-app';
import Address from '@/components/address/instance';
import type { ToastInst } from 'nutui-uniapp';
const toastRef = ref<ToastInst>() // toast
const props = defineProps({
  type: {
    type: String,
    default: 'NORMAL',
  },

  storeId: {
    type: String,
    default: ''
  },
  way: {
    type: String,
    default: ''
  }
})
const params = ref({
  pageSize: 10,
  pageNumber: 1,
})

const addressInstance = new Address(params.value)
const data = ref([])
const dataOptions = ref(addressInstance.defaultStoreOptions)

async function init() {
  uni.stopPullDownRefresh();

  const res = await addressInstance.fetchStoreList(props.storeId)
  if (res.data.success)
    data.value = res.data.result.records
}
async function handleClickAddress(event:any,val:any) {
  toastRef.value?.showToast['loading']('加载中', {
    cover: true,
    duration: 0
  })
  try {
    toastRef.value?.hideToast();
    const res = await setStoreAddressId(val.id, props.way)
    if (res.data.success) {
      uni.navigateBack()
    }
  } catch (error) {
    toastRef.value?.hideToast();
  }

}



onShow(async () => {
  params.value.pageNumber = 1
  params.value.pageSize = 10
  init()
})
onPullDownRefresh(() => {
  params.value.pageNumber = 1
  params.value.pageSize = 10
  init()
})

</script>


<style scoped></style>
